package com.cskaoyan.market.service;

import com.cskaoyan.market.bo.Register;
import com.cskaoyan.market.bo.Reset;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.util.AdminResponseCode;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.time.LocalDateTime.now;


@Service
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    MarketUserMapper userMapper;


    @Override
    public MarketUser login(String username, String password) {

        MarketUserExample userExample = new MarketUserExample();
        userExample.or().andUsernameEqualTo(username).andPasswordEqualTo(password).andDeletedEqualTo(false);
        MarketUser user = userMapper.selectOneByExample(userExample);
        return user;
    }

    @Override
    public void updateById(MarketUser user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int queryIdByUsername(String username) {
        MarketUserExample userExample = new MarketUserExample();
        userExample.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        MarketUser user = userMapper.selectOneByExample(userExample);
        return user.getId();
    }

    @Override
    public String queryPasswordByUsername(String username) {
        MarketUserExample userExample = new MarketUserExample();
        userExample.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        MarketUser user = userMapper.selectOneByExample(userExample);
        return user.getPassword();
    }

    /**
     * 注册用户
     * 先判断是否存在该用户
     *
     * @param register
     * @return
     */

    @Override
    public int register(Register register) {

        @Length(min = 5) String code = register.getCode();
        @Length(min = 8) String mobile = register.getMobile();
        @Length(min = 6) String username = register.getUsername();
        @Length(min = 8) String password = register.getPassword();
        String wxCode = register.getWxCode();

        // 先判断user表中是否有这个用户
        MarketUserExample userExample = new MarketUserExample();
        userExample.or().andDeletedEqualTo(false);
        List<MarketUser> userList = userMapper.selectByExample(userExample);
        for (MarketUser user : userList) {
            if(user.getUsername().equals(username)){
                // 存在就直接返回
                return AdminResponseCode.ROLE_USER_EXIST;
            }
        }

        // 再判断验证码是否正确
   if(!code.equals("65536")){
       return AdminResponseCode.USER_CODE_ERROR;
   }

        // 如果表中没有这个用户,则插入
        MarketUser user = new MarketUser();
        user.setMobile(mobile);
        user.setUsername(username);
        user.setPassword(password);
        user.setAddTime(now());
        user.setUpdateTime(now());
        user.setDeleted(false);
        user.setLastLoginTime(now());
        user.setWeixinOpenid(wxCode);
        userMapper.insertSelective(user);
        return 200;
    }

    @Override
    public int reset(Reset reset) {
        // 先遍历用户表中所有数据找到与mobile相匹配的数据
        MarketUserExample userExample = new MarketUserExample();
        userExample.or().andDeletedEqualTo(false).andMobileEqualTo(reset.getMobile());
        MarketUser marketUser = userMapper.selectOneByExample(userExample);
        // 表示用户不存在
        if (marketUser==null){
            return AdminResponseCode.ROLE_USER_EXIST;
            // 表示用户输入的验证码不正确
        }else if (!"65536".equals(reset.getCode())){
            return AdminResponseCode.USER_CODE_ERROR;
        }
        // 当程序运行到此处时, marketUser就指向要修改的那一列
        marketUser.setPassword(reset.getPassword());
        userMapper.updateByPrimaryKeySelective(marketUser);
        return 200;
    }

}
